﻿Namespace FileSystemScan
    Public Class RescanBlock
        Implements IDisposable
        Private mFilePath As String
        Private Shared mBlockedFiles As New Generic.Dictionary(Of String, DateTime)

        Public Sub New(ByVal filePath As String)
            AddBlock(filePath)
        End Sub
        Public Sub New(ByVal file As Entities.SharedItemFile)
            AddBlock(IO.Path.Combine(file.Directory, file.FileName))
        End Sub
        Public Sub New(ByVal file As Entities.Downloads.File)
            AddBlock(IO.Path.Combine(file.Directory, file.Name))
        End Sub
        Private Sub AddBlock(ByVal filePath As String)
            mFilePath = filePath
            SyncLock mBlockedFiles
                If mBlockedFiles.ContainsKey(filePath.ToLower) Then
                    mBlockedFiles.Item(filePath.ToLower) = Date.MaxValue
                Else
                    mBlockedFiles.Add(filePath.ToLower, Date.MaxValue)
                End If
            End SyncLock
            'Manager.OnLogEvent(String.Format("Blocking changes for file {0}", filePath))
        End Sub

        Friend Shared Function IsBlocked(ByVal filePath As String) As Boolean
            SyncLock mBlockedFiles
                If Not mBlockedFiles.ContainsKey(filePath.ToLower) Then
                    Return False
                ElseIf mBlockedFiles.Item(filePath.ToLower) < Now Then
                    Return False
                Else
                    'Manager.OnLogEvent(String.Format("Blocked rescan of {0}", filePath))
                    Return True
                End If
            End SyncLock
        End Function
        Friend Shared Sub PurgeOldBlocks()
            SyncLock mBlockedFiles
                For i As Integer = mBlockedFiles.Count - 1 To 0 Step -1
                    Dim strKey As String = mBlockedFiles.Keys(i)
                    If mBlockedFiles(strKey) < Now Then
                        mBlockedFiles.Remove(strKey)
                        'Manager.OnLogEvent(String.Format("Purged old block of {0}", strKey))
                    End If
                Next
            End SyncLock
        End Sub


        Private disposedValue As Boolean = False        ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                SyncLock mBlockedFiles
                    If mBlockedFiles.ContainsKey(mFilePath.ToLower) Then
                        mBlockedFiles.Item(mFilePath.ToLower) = Now.AddSeconds(10)
                    Else
                        mBlockedFiles.Add(mFilePath.ToLower, Now.AddSeconds(10))
                    End If
                End SyncLock
                'Manager.OnLogEvent(String.Format("Removed change block for file {0}", mFilePath))
            End If
            Me.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class
End Namespace